package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.Fnryqk;
import com.ruoyi.system.domain.FnryqkSelect;
import com.ruoyi.system.domain.Guestbookmuban;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Guestbook;
import com.ruoyi.system.service.IGuestbookService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-03-23
 */
@Controller
@RequestMapping("/system/guestbook")
public class GuestbookController extends BaseController
{
    private String prefix = "system/guestbook";

    @Autowired
    private IGuestbookService guestbookService;

    @GetMapping("/guestbooklist")
    public String guestbooklist(HttpServletRequest request)
    {

        return prefix + "/guestbooklist";
    }

    @GetMapping("/guestbookedit")
    public String guestbookedit(HttpServletRequest request)
    {

        return prefix + "/guestbookedit";
    }

    /**
     * 查询fnryqk列表
     */
    //@RequiresPermissions("system:fnryqk:customlist")
    @PostMapping("/customlist")
    @ResponseBody
    public TableDataInfo customlist(Guestbookmuban guestbookmuban)
    {
        startPage();
        List<Guestbookmuban> list = guestbookService.selectCustomGuestbookList(guestbookmuban);
        return getDataTable(list);
    }

    @RequiresPermissions("system:guestbook:view")
    @GetMapping()
    public String guestbook()
    {
        return prefix + "/guestbook";
    }

    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:guestbook:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Guestbook guestbook)
    {
        startPage();
        List<Guestbook> list = guestbookService.selectGuestbookList(guestbook);
        for(int i=0;i<list.size();i++){
            long mmZhuangtai=list.get(i).getZhuangtai();
            if(mmZhuangtai==1){
                list.get(i).setShifougongkai("公开");
            }else{
                list.get(i).setShifougongkai("未公开");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:guestbook:export")
    @Log(title = "留言", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Guestbook guestbook)
    {
        List<Guestbook> list = guestbookService.selectGuestbookList(guestbook);
        ExcelUtil<Guestbook> util = new ExcelUtil<Guestbook>(Guestbook.class);
        return util.exportExcel(list, "留言数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:guestbook:add")
    @Log(title = "留言", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Guestbook guestbook)
    {
        return toAjax(guestbookService.insertGuestbook(guestbook));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{guestbookid}")
    public String edit(@PathVariable("guestbookid") String guestbookid, ModelMap mmap)
    {
        Guestbook guestbook = guestbookService.selectGuestbookById(guestbookid);
        mmap.put("guestbook", guestbook);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:guestbook:edit")
    @Log(title = "留言", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Guestbook guestbook)
    {
        return toAjax(guestbookService.updateGuestbook(guestbook));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:guestbook:remove")
    @Log(title = "留言", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(guestbookService.deleteGuestbookByIds(ids));
    }





    // 个人反馈
    @Log(title = "guestbook", businessType = BusinessType.INSERT)
    @PostMapping("/addguestbook")
    @ResponseBody
    public AjaxResult addguestbook(Guestbook guestbook,HttpServletRequest request)
    {
        guestbook.setGuestbookid(UUID.randomUUID().toString());
        guestbook.setChuangjiantime(new Date());
        guestbook.setGengxintime(new Date());
        guestbook.setIsvalid((long)1);    //  有效
        guestbook.setZhuangtai((long)0);  //  未公开
        guestbook.setText1(getIp(request));

        return toAjax(guestbookService.insertGuestbook(guestbook));
    }

    // 更新状态
    @Log(title = "guestbook", businessType = BusinessType.INSERT)
    @PostMapping("/updatezhuangtai")
    @ResponseBody
    public AjaxResult updatezhuangtai(Guestbook guestbook)
    {
//        guestbook.setZhuangtai((long)1);
        return toAjax(guestbookService.updateGuestbook(guestbook));

    }

    public static String getIp(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
        }
        return ip;
    }

}
